﻿using FreeSql;
using System;
using System.Diagnostics;

namespace LYR_RPA.utils
{
    public class DBUtils
    {
        private static readonly string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\LYR_RPA\\";
        private static readonly string connectionString = $"Data Source={path}lyr-rpa.db;";
        private DBUtils() { }

        static Lazy<IFreeSql> sqlite = new Lazy<IFreeSql>(() =>
        {
            if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
            var fsql = new FreeSqlBuilder()
                .UseMonitorCommand(cmd => Console.WriteLine($"Sqlite：{cmd.CommandText}\n\r"))
                .UseConnectionString(DataType.Sqlite, connectionString)
                .UseAutoSyncStructure(true)
                .Build();

            // 启用日志记录
            fsql.Aop.CurdAfter += (_, e) => Console.WriteLine($"Sqlite：{e.Sql}\n\r");
            Console.WriteLine(path);
            return fsql;
        });

        public static IFreeSql Sqlite => sqlite.Value;


    }
}
